var helpYi = {};
/**
 * 实例化控制器
 * @param {Object} 控制器名称
 * @param {Object} user 用户数据
 */
helpYi.getController = function(controllerName,user){
	var Controller = null;
	switch (controllerName){
		case "Zuosa" : Controller = new Zuosa(user);break;
		case "Digu" : Controller = new Digu(user);break;
	}
	return Controller;
}
/**
 * 获取控制器中文名
 * @param {Object} controller 控制器类名
 */
helpYi.getControllerName = function(controller){
	var _blogs = yijs.config.BLOGS;
	var _name = null;
	$.each(_blogs,function(i){
		if(_blogs[i].controller == controller) _name = _blogs[i].name;
	})
	return _name;
}
/**
 * 获取视图路径
 * @param {Object} viewName 视图名称
 */
helpYi.getViewPath = function(viewName){
	return yijs.config.basePath+yijs.VIEWS+"/"+yijs.config.view+"/"+viewName;
}
/**
 * 获取语言xml字符串
 * @param {Object} data 语言数据
 */
getLangXml = function(data){
	var lang = "";
	var xml = "";
	_each(data);
	xml = "<lang>"+lang+"</lang>";
	
	function _each(data){
		if(data && typeof data == "object"){
			$.each(data,function(key,value){
				if(typeof value == "object"){
					lang += "<"+key+">";
					_each(value);
					lang += "</"+key+">";
				}else{
					lang += "<"+key+">"+value+"</"+key+">";
				}
				
			})
		}		
	}
	
	return xml;	
}
/**
 * 视图切换效果
 */
helpYi.viewChangeEffect = {
	/**
	 * 渐隐
	 * @param {String} renderTo 父容器
	 * @param {String} html     向父容器插入的内容
	 * @param {String/Number} speed	速度
	 */
	fade : function(renderTo,html,speed){
		var speed = speed || "slow";
		$(renderTo).append(html);
		helpYi.viewChangeEffect.setPositionAbsolute(renderTo,html);
		var $children = $(renderTo).children();
		if($children.size() > 1){
			$(renderTo).children().eq(0).fadeOut(speed,function(){
				$(this).remove();
			})			
		}
	},
	setPositionAbsolute : function(renderTo){
		var $renderTo = $(renderTo);
		var index = 100;
		if($renderTo.size() > 0){
			$renderTo.css("position","relative");
			var $children = $renderTo.children(".MAIN");
			if($children.size() > 0){
				$children.each(function(){
					index--;
					$(this).css({"position":"absolute","top":"0px","left":"0px","zIndex":index});
				})
			}
		}
	}
}
/**
 * ajax预加载
 */
helpYi.preLoad ={
	/**
	 * 显示预加载层
	 */
	show : function(msg){
		if ($("#PRE_LOAD").size() == 0) {
			helpYi.preLoad.create();
			helpYi.preLoad.center();
		}
		var $preload = $("#PRE_LOAD");
		helpYi.preLoad.msg(msg || yijs.lang.ajaxMsg.defaultMSg || "");
	    $preload.css("display") == "none" && $preload.stop().fadeIn("fast");	
	},
	/**
	 * 创建预加载层
	 */	
	create : function(){
		var html = "<div id='PRE_LOAD'>"+
		                "<div id='PRE_LOAD_IMG' /><div id='PRE_LOAD_MSG'></div>"+
				   "</div>";	   
		$("body").append(html);   
		/*
		var xml = "<preload></preload>";
		var xsl = getViewPath(yijs.config.viewName.preLoad);
		yijs.air.view(xml,{url:xsl},"body",function(xml,xsl){},true);
		*/			
	},
	/**
	 * 隐藏预加载层
	 */		
	hide : function(delayTimer,callback){
		if ($("#PRE_LOAD").size() > 0) {
			delayTimer && $("#PRE_LOAD").delay(delayTimer).fadeOut("fast",function(){callback && callback.call(this)}) || $("#PRE_LOAD").fadeOut("fast");
		}	
	},
	/**
	 * 设置预加载层显示信息
	 */		
	msg : function(message){
		$("#PRE_LOAD_MSG").html(message);
	},
	/**
	 * 居中预加载层
	 */		
	center : function(){
		var width = $("#PRE_LOAD").innerWidth();
		var height = $("#PRE_LOAD").innerHeight();
		var marginLeft = width/2;
		var marginTop =  height/2;
		var bodyWidth = $("body").width();
		var bodyHeight = $("body").height();
		var w = Math.floor(bodyWidth/2 - marginLeft);
		var h = Math.floor(bodyHeight/2 - marginTop);
		$("#PRE_LOAD").css({"top":h,"left":w});
	},
	ajaxing : false
}

/**
 * 格式化输出时间
 */
helpYi.formatTime = function(t){
	var timer = $.trim(t.split(",")[1]);
	var values = timer.split(" ");
	time_value = values[1] + " " + values[0] + ", " + values[2] + " " + values[3];
	var parsed_date = Date.parse(time_value)-1000*60*60*8; //we use +0800 timezone.
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);
	if(delta < 60){
		return "少于1分钟";
	}else if(delta < 120){
		return "约1 分钟前";
	}else if(delta < (45*60)){
		return (parseInt(delta / 60)).toString() + "分钟前";
	}else if(delta < (90*60)){
		return "约1 小时前";
	}else if(delta < (24*60*60)){
		return "约" + (parseInt(delta / 3600)).toString() + "小时前";
	}else if(delta < (48*60*60)){
		return "1天前";
	}else{
		return (parseInt(delta / 86400)).toString() + "天前";
	}	
}

/**
 * ajax代理
 * @param {Object} options 参数
 */
helpYi.proxy = function(options){
		var _msgs 		   = yijs.lang.ajaxMsg;
		var _beforeSendMsg = yijs.lang.ajaxMsg.defaultMSg;
		var _successMsg    = null;
		if(options.preLoadMsg){
			_beforeSendMsg = yijs.setTpl(_msgs.get,[options.preLoadMsg]);
			_successMsg = yijs.setTpl(_msgs.complete,[options.preLoadMsg]);			
		}
		$.ajax({
		    url : options.url,
		    method : options.method || 'GET',
			dataType : options.dataType || 'xml',
			data : options.data || {},
		    beforeSend : function(req) {
		        req.setRequestHeader('Authorization', options.Authorization);
				helpYi.preLoad.ajaxing = true;
				helpYi.preLoad.show(_beforeSendMsg);
		    },
			success : function(data){
				helpYi.preLoad.ajaxing = false;
				_successMsg && helpYi.preLoad.msg(_successMsg);
				if(!helpYi.preLoad.ajaxing){
					helpYi.preLoad.hide(800,function(){
						if(options.success) options.success.call(this,data);
					});					
				}
				
				var $error = $(data).find("error");
				$error.size() > 0 && alert(helpYi.getAjaxError($error.text()));
			},
			complete : function(){
				helpYi.preLoad.hide(4000);
			}
		});		
}
/**
 * 获取ajax错误信息
 * @param {Object} code 错误码
 */
helpYi.getAjaxError = function(code){
	var code = Number(code);
	var errorMsgs = yijs.lang.ajaxMsg.errors;
	$.each(errorMsgs,function(i){
		if(Number(errorMsgs[i][0]) == code) return errorMsgs[i][1];
	})
	return yijs.lang.ajaxMsg.defaultError;
}

helpYi.json2xml = function(json,rootNode,itemNode,filter){
	var xml = "<"+rootNode+">";
	$.each(json,function(i){
		xml += "<"+itemNode+">";
		$.each(json[i],function(key,value){
			var _val = value;
			xml += "<"+key+">";
			if(filter){
				_val = filter.call(this,key,value);
			}
			xml += "<![CDATA[" + _val +"]]>";
			xml += "</"+key+">";
		})
		xml += "</"+itemNode+">";
	})
	xml += "</"+rootNode+">";
	var domParser = new DOMParser();
	return domParser.parseFromString(xml,"text/xml");
}
/**
 * xml转成json
 * @param {Object} parentNode 根节点
 * @return {Object} json数据
 */
helpYi.xml2json = function(parentNode){
	var _json = {};
	$(parentNode).children().each(function(){
		var nodeName    = $(this).get(0).nodeName;
		var value	    = $(this).text();
		_json[nodeName] = value;
	})
	return _json;
}
/**
 * 保留需要的字段数据，组成个关联数组
 * @param {Object} arr	字段名
 * @param {Object} $node 遍历的节点
 */
helpYi.formatFieldsJson = function(arr,$node){
	var data = {};
	if(arr && arr.length > 0){
		$.each(arr,function(i){
			$node.each(function(){
				var _nodeName = $(this).get(0).nodeName;
				if(_nodeName == arr[i]){
					data[_nodeName] = $(this).text();
					return false;
				} 
			})				
		})
		
	}
	return data;	
}
/**
 * 反转json数据
 * @param {Object} json json数据
 * @return {Object}     json数据
 */
helpYi.jsonReverse = function(json){
	var _j = [];
	if(json.length > 0){
		for(var i = json.length-1;i >= 0;i--){
			var _o = {};
			$.each(json[i],function(key,value){
				_o[key] = value;
			})
			_j.push(_o);
		}
	}
	return _j;
}
helpYi.formatFace = function(url,size){
	var _face = url;
	var _reg = /24x24/;
	_face = _face.replace(_reg,size);	
	return _face;
}

